package com.xxx.springboot.util;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

/**
 * hutool小工具类
 * 
 * @author guweiqiang
 * @date 2020年7月13日
 */
public class HutoolUtil {

	private static final String DEFAULT_KEY = "abCDEFxxx@123456";  // AES key
	private static final String IV = "1234567887654321";	// iv便宜量

	/**
	 * 判断手机号是否合法
	 */
	public static boolean isMobile(String mobile) {
		return Validator.isMobile(mobile);
	}
	
	/**
	 * 判断邮箱是否合法
	 */
	public static boolean isEmail(String email) {
		return Validator.isEmail(email);
	}

	/**
	 * 判断URL是否合法
	 */
	public static boolean isUrl(String url) {
		return Validator.isUrl(url);
	}

	/**
	 * 判断生日是否合法
	 */
	public static boolean isBirthday(String birthday) {
		return Validator.isBirthday(birthday);
	}
	
	/**
	 * 判断是否为ip4
	 */
	public static boolean isIp4(String ip) {
		return Validator.isIpv4(ip);
	}
	
	/**
	 * 判断是否为ip6
	 */
	public static boolean isIp6(String ip) {
		return Validator.isIpv6(ip);
	}

	/**
	 * 判断是否为纯数字
	 */
	public static boolean isNumber(String str) {
		return Validator.isNumber(str);
	}

	/**
	 * 判断是否为纯字母（包含希腊字母等非英文字母）
	 */
	public static boolean isLetter(String str) {
		return Validator.isLetter(str);
	}

	/**
	 * 判断是否为纯英文字母
	 */
	public static boolean isEnglish(String str) {
		return Validator.isWord(str);
	}

	/**
	 * 判断是否为纯中文
	 */
	public static boolean isChinese(String str) {
		return Validator.isChinese(str);
	}

	/********************************************* AES加密 *************************************************/
	private static String getEncodeKey(String key){
		if(StringUtils.isEmpty(key)) {
			return DEFAULT_KEY;
		}
		return key;
	}
	/**
	 * AES加密
	 */
	public static String encrypt(String data){
		AES aes = new AES(Mode.CBC, Padding.ZeroPadding, new SecretKeySpec(getEncodeKey(null).getBytes(), "AES"), new IvParameterSpec(IV.getBytes()));
		return aes.encryptBase64(data, StandardCharsets.UTF_8);
	}
	public static String encryptByAES(String data, Mode mode, Padding padding){
		AES aes;
		if (Mode.CBC == mode) {
			aes = new AES(mode, padding, new SecretKeySpec(getEncodeKey(null).getBytes(), "AES"), new IvParameterSpec(IV.getBytes()));
		} else {
			aes = new AES(mode, padding, new SecretKeySpec(getEncodeKey(null).getBytes(), "AES"));
		}
		return aes.encryptBase64(data, StandardCharsets.UTF_8);
	}

	/**
	 * AES解密
	 */
	public static String decrypt(String encryptData){
		AES aes = new AES(Mode.CBC, Padding.ZeroPadding, new SecretKeySpec(getEncodeKey(null).getBytes(), "AES"), new IvParameterSpec(IV.getBytes()));
		byte[] decryptDataBase64 = aes.decrypt(encryptData);
		return new String(decryptDataBase64, StandardCharsets.UTF_8);
	}
	public static String decryptByAES(String encryptData, Mode mode, Padding padding){
		AES aes;
		if (Mode.CBC == mode) {
			aes = new AES(mode, padding, new SecretKeySpec(getEncodeKey(null).getBytes(), "AES"), new IvParameterSpec(IV.getBytes()));
		} else {
			aes = new AES(mode, padding, new SecretKeySpec(getEncodeKey(null).getBytes(), "AES"));
		}
		byte[] decryptDataBase64 = aes.decrypt(encryptData);
		return new String(decryptDataBase64, StandardCharsets.UTF_8);
	}

	/**
	 * unicode转中文
	 */
	public static String unicode2Str(String uniStr){
		return UnicodeUtil.toString(uniStr);
	}

	/**
	 * main
	 * @param args
	 */
	public static void main(String[] args) {
//		System.out.println("校验手机号合法性：");
//		System.out.println(isMobile("18600012641"));
//		System.out.println(isMobile("12600014042"));
//
//		System.out.println("校验邮箱合法性：");
//		System.out.println(isEmail("guwq@58.com"));
//
//		System.out.println("校验ip合法性：");
//		System.out.println(isIp4("guwq@58.com"));
//		System.out.println(isIp4("117.0.2.1"));

//		System.out.println("校验url合法性：");
//		System.out.println(isUrl("https://blog.csdn.net/qq_48575500/article/details/122899287"));
//		System.out.println(isUrl("http://www.rj-jk.com"));
//		System.out.println(isUrl("000752.com/sy"));
//		System.out.println(isUrl("www.chinayinyi.cn/www.sensteed.com"));
//
//		System.out.println("校验生日合法性：");
//		System.out.println(isBirthday("2020.1.11"));
//		System.out.println(isBirthday("2020.09-10"));

//		String data = "Tq8dGvvkThIx9UqWavQRJh4bItXFIKmHMiGkwGO4SA1eLDo6XSudo5zkzaNvoTF2";
//		String encryptData = encryptByAES(data, Mode.CBC, Padding.ZeroPadding);
//		System.out.println("加密：" + encryptData);
//		String decryptData = decryptByAES(encryptData, Mode.CBC, Padding.ZeroPadding);
//		System.out.println("解密：" + decryptData);

//		System.out.println(isNumber("01234567"));
//		System.out.println(isNumber("01234567我"));
//		System.out.println(isNumber("01234和567"));
//		System.out.println(isNumber("你01234567"));
//		System.out.println(isNumber("01234567八"));

		System.out.println(isLetter("α"));
		System.out.println(isLetter("abcD"));
		System.out.println(isLetter("abc1"));
		System.out.println(isLetter("012Abc"));

//		System.out.println(isEnglish("α"));
//		System.out.println(isEnglish("abcD"));
//		System.out.println(isEnglish("abc1"));
//		System.out.println(isEnglish("012Abc"));

//		System.out.println(isChinese("01234567"));
//		System.out.println(isChinese("01234567我"));
//		System.out.println(isChinese("01234和567"));
//		System.out.println(isChinese("你01234567"));
//		System.out.println(isChinese("01234567八"));
//		System.out.println(isChinese("你好"));

//		System.out.println(unicode2Str("a我1\u5468"));
//		System.out.println(unicode2Str("\u5468"));

	}

}
